package com.esunrising.esungsc.service.impl;

import com.esunrising.esungsc.dao.FIGLAYwdjDao;
import com.esunrising.esungsc.entity.FIGLAYwdj;
import com.esunrising.esungsc.service.FIGLAYwdjService;
import com.esunrising.esungsc.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;


@Service
public class FIGLAYwdjServiceImpl extends BaseServiceImpl<FIGLAYwdjDao, FIGLAYwdj> implements FIGLAYwdjService {

    @Resource
    private FIGLAYwdjDao dao;

    @Override
    public void syncSave(FIGLAYwdj entity) {
        // 如果存在，则先删除
        Optional<FIGLAYwdj> byId = dao.findById(entity.getId());
        byId.ifPresent( doc -> {
            dao.deleteById(doc.getId());
        });
        // 保存数据
        super.save(entity);
    }

    @Override
    public FIGLAYwdj findById(String id){
        Optional<FIGLAYwdj> byId = dao.findById(id);
        return byId.orElse(null);
    }

    /**
     * GSC凭证关联业务单据数组存入数据库，其中ID重复的会覆盖
     *
     * @param ywdjList GSC凭证关联业务单据数组
     */
    @Override
    public void syncFIGLAYwdjList(List<FIGLAYwdj> ywdjList) {
        for (FIGLAYwdj figlaYwdj : ywdjList) {
            save(figlaYwdj);
        }
    }
}